package org.jfunction.ui.internal.editor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.fieldassist.IContentProposal;
import org.eclipse.jface.fieldassist.IContentProposalProvider;

public class AutoComplete implements IContentProposalProvider {
	private String[] data;

	public AutoComplete(final String[] data) {
		this.data = data;
	}

	@Override
	public IContentProposal[] getProposals(final String content, int position) {
		List<IContentProposal> cpList = new ArrayList<IContentProposal>(64);
		for (int i = 0; i < this.data.length; ++i) {
			final String candidate = this.data[i];
			if (isContentProposal(candidate, content)) {
				cpList.add(new IContentProposal(){

					@Override
					public String getContent() {
						return candidate;
					}

					@Override
					public int getCursorPosition() {
						return candidate.length();
					}

					@Override
					public String getLabel() {
						return candidate;
					}

					@Override
					public String getDescription() {
						return null;
					}
				});
			}
		}
		IContentProposal[] cpArray = new IContentProposal[cpList.size()];
		return (IContentProposal[]) cpList.toArray(cpArray);
	}

	private boolean isContentProposal(String candidate, String content) {
		if ("".equals(candidate) || "".equals(content)) {
			return false;
		}
		return candidate.toLowerCase().startsWith(content.toLowerCase());
	}
}

class FieldAssistUtil {
	private FieldAssistUtil() {
	}

	public static final char[] getAlphaNumericChars() {
		String alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		String deleteChar = new String(new char[] { 8 });

		String combo = alphanum + deleteChar;

		return combo.toCharArray();
	}
}
